package com.pig.pcart.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.pig.pcart.service.CartService;
import com.pig.pojo.Cart;
import com.pig.util.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @RequestMapping("/cart/show")
    public SysResult testJSONP(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        Integer userId=77;//暂时写死，模拟数据
        List<Cart> cartList=cartService.findUserItems(userId);
        return SysResult.success(cartList);
    }

    /** 购物车点击减号修改商品数量-1
     *  url:"http://127.0.0.1:8891/cat/changNum"
     */
    @RequestMapping("/cart/changNum")
    public SysResult changNum(Integer num,String cartId, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        Integer cartId1= Integer.parseInt(cartId);
        Cart cart=new Cart();
        cart.setNum(num);
        cart.setCartId(cartId1);
        cartService.changeNum(cart);
        return SysResult.success();
    }
}
